package cn.tedu.logistics.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;

import cn.tedu.logistics.bean.InputInfo;
import cn.tedu.logistics.bean.OrderInfo;
import cn.tedu.logistics.service.OrderService;

@Controller
@RequestMapping("/order")
public class OrderController {

	@Autowired
	private OrderService orderService;
	List<OrderInfo> list=null;
	

	Date date = new Date();
	DateFormat format = new SimpleDateFormat("yyyy-MM-dd");// 设定日期格式为yyyy-MM-dd

	@RequestMapping("/findAll")
	public String findAll(ModelMap map) {
		list = orderService.findAll();
		map.addAttribute("list", list);
		return "Order";
	}

	@RequestMapping("/udOrder") // 更改数据
	public String updOrder(OrderInfo orderInfo) throws ParseException {
		date = format.parse(format.format(date));
		orderInfo.setUpdatetime(date);// 为更改时间赋值
		orderService.updateOrder(orderInfo);// 更改数据
		return "redirect:findAll";
	}
	
	@RequestMapping(value = "/selectById")
	@ResponseBody
	public Map<String, Object> selectById(OrderInfo order) throws JsonProcessingException {
		Map<String, Object> map = new HashMap<String, Object>();
		list = orderService.getOrder(order);
		if (list == null || list.size() == 0) {// 数据库是否有数据
			Integer integer = 2;
			map.put("integer", integer);

		} else {
			map.put("list", list);
			Integer integer = 1;
			map.put("integer", integer);
		}
		return map;
	}

	@RequestMapping("/orderadd")
	public String showReborrow() {
		return "order_add";
	}
	
	@RequestMapping("/showudporder") // 根据ID查询数据
	public String toUdpBook(String orderid, Model model) {
		OrderInfo orderInfo = orderService.getorderById(orderid);
		model.addAttribute("list", orderInfo);
		return "order_udpate";
	}
	
	@RequestMapping("/addorder") // 新增数据
	public String addBook(OrderInfo orderInfo) throws ParseException {

		date = format.parse(format.format(date));

		Random random = new Random();
		String result = "";
		for (int i = 0; i < 10; i++) {
			result += random.nextInt(10);
		}

		orderInfo.setOrderid(result);
		orderInfo.setUpdatetime(date);// 为创建时间和更改时间赋值
		orderInfo.setCreatetime(date);// 为创建时间和更改时间赋值
		orderService.addorder(orderInfo);// 新增数据
		return "redirect:findAll";
	}
	@RequestMapping("/confirmOrder")
	public String confirmOrder(String orderid) {
		orderService.confirm(orderid);
		return "redirect:findAll";
	}
	@RequestMapping("/executeOrder")
	public String executeOrder(String orderid) {
		orderService.execute(orderid);
		return "redirect:findAll";
	}

}
